/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package researchgrants.parts.LoggedData.panels;

import com.toedter.calendar.JDateChooser;
import java.awt.Component;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JLabel;
import javax.swing.event.ChangeEvent;
import researchgrants.parts.LoggedData.LogableDate;
import researchgrants.parts.LoggedData.LogableObject;
import researchgrants.parts.date.DateForDb;

/**
 *
 * @author DOStudent1
 */
public class LoggedDataPanelsDate extends LoggedDataPanels {

    
    
    @Override
    public Component getViewComponent() {
        DateForDb date=null;
        JLabel lblViewData=new JLabel();
        if (!getData().value().equals("")) {
            LogableDate logableDate=(LogableDate)getData().value();
            date = DateForDb.fromDate(logableDate.getDate());
            lblViewData.setText(date.toDisplayString());
        }
        return(lblViewData);
    }

    JDateChooser dateChooser;
            
    @Override
    public Component getEditComponent() {
        dateChooser=new JDateChooser();
        dateChooser.addPropertyChangeListener("date", new DateChooserListener());
        if (hasDataInitialized()) {
            if (!getData().value().equals("")) {
                LogableDate logableDate=(LogableDate)getData().value();
                DateForDb date = DateForDb.fromDate(logableDate.getDate());
                dateChooser.setDate(date.toDate());
            }
        }
        return(dateChooser);
    }

    @Override
    public LogableObject getEdittedValue() {
        Date newDate=dateChooser.getDate();
        return(new LogableDate().fromDate(newDate));
    }

    @Override
    public boolean isEmpty() {
        return(dateChooser.getDate()==null);
    }

    private class DateChooserListener implements PropertyChangeListener {

        // To alleviate the non-interactiveness of the JDateChooser object, 
        // I manually raise a change every few milliseconds
        public DateChooserListener() {
            TimerTask task=new TimerTask() {

                @Override
                public void run() {
                    propertyChange(new PropertyChangeEvent(this, "date", null, null));
                }
            };
            Timer timer=new Timer();
            timer.scheduleAtFixedRate(task, 0, 100);
        }

        public void propertyChange(PropertyChangeEvent evt) {
            notifyAllChangeListeners(new ChangeEvent(evt.getSource()));
        }

    }

    @Override
    public boolean isValid() {
        return(true);
    }

//    @Override
//    public void saveData() {
//        if (getData()==null) {
//            // saving data for the first time
//            Date newDate=dateChooser.getDate();
//            LogableDate logableDate=new LogableDate(newDate);
//            HistoricallyLoggedData newData=HistoricallyLoggedData.createNew(logableDate);
//            setData(newData);
//            notifyAllSaveValidationListeners(new SaveValidationEvent(this));
//        } else {
//            Date newDate=dateChooser.getDate();
//            LogableDate logableDate=new LogableDate(newDate);
//            // saving only when the data had changed
//            if (!getData().value().equals(logableDate)) {
//                // saving data for an existing HistoricallyLoggedData
//                // should be called only if data is available (when the user is allowed to edit)
//                getData().setNewValue(logableDate);
//                notifyAllSaveValidationListeners(new SaveValidationEvent(this));
//            }
//        }
//    }
    
}
